package com.itheima.tlias84.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.itheima.tlias84.config.AliyunOssConfig;
import com.itheima.tlias84.config.OssClient;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {
//    LTAI5tPxzeVNt3p6KSMN6j8W         id
//    YDg9ui6lG75Tw9JKrUwoKpFAYEN09z   secret

    @Resource
    OssClient ossClient;

    private static final Logger log = LoggerFactory.getLogger(UploadController.class);

    /**
     * MultipartFile  Spring封装的用于接受文件的对象
     * @param file
     * @return
     */
    @PostMapping
    public ResultUtil upload(MultipartFile file) throws IOException {
        //获取参数名
        log.info("name{}", file.getName());
        //获取实际上传文件名
        log.info("originalFilename{}", file.getOriginalFilename());
        //拆分文件后缀(类型)
        String filename = file.getOriginalFilename();
        String url = ossClient.upload(filename, file.getInputStream());
        return ResultUtil.success("上传成功",url);
    }
}
